:art: Make thumbnail for image upload

huangqimin001 5 mēneši atpakaļ
vecāks
revīzija
405d666c64
2 mainītis faili ar 30 papildinājumiem un 8 dzēšanām
  1. 8 3
      kodo/oauth_settings.py
  2. 22 5
      utils/qiniucdn.py

+ 8 - 3
kodo/oauth_settings.py

@@ -251,7 +251,10 @@ def DJANGO_FILE_UPLOAD_CALLBACK_FUNC(request, file_path=None, file_url=None):
251 251
     from utils.redis.rimage import set_image_data
252 252
     file_type = request.POST.get('file_type', '')
253 253
     upload_qiniu = request.POST.get('upload_qiniu', '')
254
-    compress = bool(request.POST.get('compress', 0))
254
+    compress = bool(request.POST.get('compress', 1))
255
+    thumbnail = bool(request.POST.get('thumbnail', 1))
256
+    thumbnailw = int(request.POST.get('thumbnailw') or request.POST.get('w') or 1080)
257
+    thumbnailh = int(request.POST.get('thumbnailh') or request.POST.get('h') or 720)
255 258
 
256 259
     if file_type == 'logfile':
257 260
         optor_id = request.POST.get('optor_id', '')
@@ -268,7 +271,7 @@ def DJANGO_FILE_UPLOAD_CALLBACK_FUNC(request, file_path=None, file_url=None):
268 271
         height = request.POST.get('height', 0)
269 272
         file = request.FILES.get('file', '')
270 273
         # upload_file_path('media/' + file_path, file_path, bucket='tamron')
271
-        upload_file_req(file, file_path, bucket='tamron', compress=compress)
274
+        file_path = upload_file_req(file, file_path, bucket='tamron', compress=compress, thumbnail=thumbnail, thumbnailw=thumbnailw, thumbnailh=thumbnailh)
272 275
         file_url = qiniu_file_url(file_path, bucket='tamron')
273 276
 
274 277
         set_image_data(file, file_path=file_path, file_url=file_url)
@@ -276,18 +279,20 @@ def DJANGO_FILE_UPLOAD_CALLBACK_FUNC(request, file_path=None, file_url=None):
276 279
         return {
277 280
             'width': width,
278 281
             'height': height,
282
+            'file_path': file_path,
279 283
             'file_url': file_url,
280 284
         }
281 285
 
282 286
     elif file_type == 'member_activity' or (file_type and upload_qiniu):
283 287
         file = request.FILES.get('file', '')
284 288
         # upload_file_path('media/' + file_path, file_path, bucket='tamron')
285
-        upload_file_req(file, file_path, bucket='tamron', compress=compress)
289
+        file_path = upload_file_req(file, file_path, bucket='tamron', compress=compress, thumbnail=thumbnail, thumbnailw=thumbnailw, thumbnailh=thumbnailh)
286 290
         file_url = qiniu_file_url(file_path, bucket='tamron')
287 291
 
288 292
         set_image_data(file, file_path=file_path, file_url=file_url)
289 293
 
290 294
         return {
295
+            'file_path': file_path,
291 296
             'file_url': file_url,
292 297
         }
293 298
 

+ 22 - 5
utils/qiniucdn.py

@@ -1,8 +1,13 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
3
+import re
4
+
3 5
 import pngquant
4 6
 import qiniu
5 7
 from django.conf import settings
8
+from django_file_md5 import calculate_data_md5
9
+
10
+from utils.thumbnail_utils import make_thumbnail2
6 11
 
7 12
 
8 13
 QINIU = settings.QINIU
@@ -10,34 +15,46 @@ auth = qiniu.Auth(QINIU['access_key'], QINIU['secret_key'])
10 15
 pngquant.config(settings.PNG_QUANT_FILE)
11 16
 
12 17
 
13
-def upload(data, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True):
18
+def generate_new_key(key, data):
19
+    # key = re.sub(r"(.+/)(.+)\.(.+)", lambda m: '{}{}.{}'.format(m.group(1), calculate_data_md5(data), m.group(3)), key)
20
+    sections = re.split(r'/|\.', key)
21
+    base_path, ext = '/'.join(sections[:-2]), sections[-1]
22
+    return '{}{}{}.{}'.format(base_path, base_path and '/', calculate_data_md5(data), ext)
23
+
24
+
25
+def upload(data, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True, thumbnail=True, thumbnailw=1080, thumbnailh=720):
14 26
     if not data:
15 27
         return ''
28
+    if thumbnail:
29
+        w, h = thumbnailw or 1080, thumbnailh or 720
30
+        data = make_thumbnail2(data, w=w, h=h)
16 31
     if compress:
17 32
         try:
18 33
             data = pngquant.quant_data(data, ndeep=3)[1]
19 34
         except Exception as e:
20 35
             pass
36
+    if (thumbnail or compress) and key:
37
+        key = generate_new_key(key, data)
21 38
     token = auth.upload_token(bucket, key=key)
22 39
     ret, _ = qiniu.put_data(token, key, data, mime_type=mime_type)
23 40
     return ret.get('key')
24 41
 
25 42
 
26
-def upload_file_admin(obj, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True):
43
+def upload_file_admin(obj, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True, thumbnail=True, thumbnailw=1080, thumbnailh=720):
27 44
     # Django Admin Upload
28 45
     if not obj.image:
29 46
         return ''
30 47
     obj.image.seek(0)
31
-    return upload(obj.image.read(), key=key or obj.image.name, mime_type=mime_type, bucket=bucket, compress=compress)
48
+    return upload(obj.image.read(), key=key or obj.image.name, mime_type=mime_type, bucket=bucket, compress=compress, thumbnail=thumbnail, thumbnailw=thumbnailw, thumbnailh=thumbnailh)
32 49
 
33 50
 
34
-def upload_file_req(file, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True):
51
+def upload_file_req(file, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True, thumbnail=True, thumbnailw=1080, thumbnailh=720):
35 52
     # photo = request.FILES.get('photo', '')
36 53
     # <InMemoryUploadedFile: photo.png (image/png)>
37 54
     if not file:
38 55
         return ''
39 56
     file.seek(0)
40
-    return upload(file.read(), key=key or file.name, mime_type=mime_type, bucket=bucket, compress=compress)
57
+    return upload(file.read(), key=key or file.name, mime_type=mime_type, bucket=bucket, compress=compress, thumbnail=thumbnail, thumbnailw=thumbnailw, thumbnailh=thumbnailh)
41 58
 
42 59
 
43 60
 def upload_file_path(path, key=None, mime_type='application/octet-stream', bucket=QINIU['bucket_default'], compress=True):